Skip to content

perf(hub): 停顿日志带路由归因 + 复用 McpServer(本地 30ms→2ms CPU/请求) - #3792

Closed
vansin wants to merge 4 commits into
mainfrom
feat/hub-stall-attribution-mcp-pool
Closed

vansin wants to merge 4 commits into
mainfrom
feat/hub-stall-attribution-mcp-pool

Conversation

@vansin

@vansin vansin commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

背景(线上实测,只读排查)

生产 Hub(bun 1.4.4,runtime .295)主线程基本吃满一个核(87–102%),每 3–5 秒一次 [commhub stall],/health 里 sessions 455 / sse 116,stall 窗口 served 90–190 req ⇒ 约 28 req/s。
GET /health 本机 curl 0.19 / 3.13 / 3.19 / 0.28 / 4.47 秒 ⇒ 不是某一条慢,是整机饱和。

根因(本地 455 会话线上库副本复现)

每条 POST /mcp 都 new McpServer() 并重新注册 105 个工具(zod → JSON Schema)。实测:

请求 修复前 CPU/请求 修复后
JSON-RPC ping(完全不跑工具) 30.0 ms 8.0 ms
tools/call 不存在的工具 28.5 ms 1.8 ms
tools/call get_inbox 24.3 ms 2.5 ms
tools/call report_status 35.3 ms 2.0 ms
tools/call get_all_status(alias=1) 37.0 ms 1.5 ms
对照 GET /api/session-requests 1.3 ms 1.0 ms

成本与「跑哪个工具」无关(ping 也要 30 ms)⇒ 全部花在重复注册工具上。28 req/s × 30 ms ≈ 840 ms/s ≈ 84% 一个核,与线上 CPU 曲线吻合。

改动

  1. route-timing.ts:新增 tick 窗口累计(每请求 次数 / 总耗时 / 单次最大;键=路由形状,MCP 带工具名;封顶 256 键折 (other))。
  2. event-loop-lag.ts:每个 tick 抽干该窗口(只清空,不排序),真停顿那次才排序取前 5;stall 日志追加 | top: <route> ×N <total>ms max <max>ms; …;/health 的 recent_stalls[].top_routes 同步带上。未接归因源时行为与旧版一致。
  3. server.ts:按调用方上下文(clientIP / 网络 / userId / callerAlias / 是否节点令牌 / tokenId / 是否要协议工具 / 身份被拒)复用已注册的 McpServer;池上限 64;并发拿不到空闲实例就现建;close() 成功才回池,失败丢弃。

测试

  • 新增 mcp-server-pool-http.test.ts(4 条,真 HTTP):同上下文复用 / 换身份不复用 / 出错后复用仍能服务 / 10 并发不打死池子。
  • 新增归因断言 7 条(route-timing.test.ts 2 条 + event-loop-lag.test.ts 5 条)。
  • mcp-inflight.test.ts 的源码断言跟上新的 close 结算形状(仍是 close 完成后结算、仍幂等)。

未做 / 需要拍板

  • 部署时间由负责人安排(重启会触发 400 节点重连风暴)。
  • 次要项(未含在本 PR):get_all_status 缓存命中率仅 0.8%(TTL 10s vs 调用间隔 60s);GET /api/nodes 889KB、/api/status 737KB 的大载荷成本。

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 23, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-23T03:57:21.313138Z 408c2eb PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 408c2ebea4

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread server/src/server.ts
Comment on lines +323 to +324
const server = idle && idle.length ? idle.pop()! : null;
if (server) { serverPoolSize--; serverReused++; return { server, key }; }

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Refresh permission snapshots before reusing an MCP server

When a member's agent_access changes from all to granted, subsequent requests retain the same pool key and reuse handlers with the old permissions. registerTools() in server/src/tools.ts snapshots restrictedNets at construction, and resolveReadScope() uses that snapshot to apply agent visibility restrictions. I reproduced this through real HTTP requests against a temporary database: after restricting a member with no agent grants, get_all_status still returned the ungranted session, with pool counters confirming reuse. This persists beyond the response-cache TTL because the permission snapshot itself is stale. Recompute mutable authorization state per request or invalidate pooled instances when permissions change.

Useful? React with 👍 / 👎.

Comment thread server/src/server.ts
Comment on lines +323 to +324
const server = idle && idle.length ? idle.pop()! : null;
if (server) { serverPoolSize--; serverReused++; return { server, key }; }

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Remove pool entries when their last idle server is acquired

When requests from different caller contexts overlap near the pool limit, popping the last server leaves an empty array in serverPool. Another context can then fill the available slot before the original request finishes, causing releaseServer() to discard the original server while retaining its empty map entry indefinitely. Consequently, the 64-server limit does not bound the map or its serialized identity keys as contexts churn. Exercising the actual pool implementation with this ordering produced 10,064 map entries, including 10,000 empty entries, while serverPoolSize remained 64. Delete a context's map entry when its idle array becomes empty so long-running hubs do not accumulate this bookkeeping without limit.

Useful? React with 👍 / 👎.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant